package mscs.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class ReadFile {
	Class readClass;
	File file;
	public ReadFile(Class readClass, File file) {
		this.readClass = readClass;
		this.file = file;
	}
	public List getClassArray() {
		ArrayList<Object> list = new ArrayList<Object>();
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
			String text = "";
			while((text = bufferedReader.readLine()) != null) {
				if (text.trim().equals(""))
					continue;
				Object instance = readClass.newInstance();
				String[] argsArray = text.split(",");
				Class[] argClasses = new Class[1];
				Object[] args = new Object[1];
				args[0] = argsArray;
				argClasses[0] = argsArray.getClass();
				Method method = readClass.getMethod("setContent", argClasses);
				Object result =  method.invoke(instance, args);
				list.add(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
